package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.CardEmptycard;
import com.ruoyi.system.domain.CardScheme;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CardMemberInfoMapper;
import com.ruoyi.system.domain.CardMemberInfo;
import com.ruoyi.system.service.ICardMemberInfoService;
import org.springframework.util.ObjectUtils;

/**
 * 会员信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-25
 */
@Service
public class CardMemberInfoServiceImpl implements ICardMemberInfoService
{
    @Autowired
    private CardMemberInfoMapper cardMemberInfoMapper;

    /**
     * 查询会员信息
     *
     * @param cardId 会员信息主键
     * @return 会员信息
     */
    @Override
    public CardMemberInfo selectCardMemberInfoByCardId(Long cardId)
    {
        return cardMemberInfoMapper.selectCardMemberInfoByCardId(cardId);
    }

    /**
     * 查询会员信息列表
     *
     * @param cardMemberInfo 会员信息
     * @return 会员信息
     */
    @Override
    public List<CardMemberInfo> selectCardMemberInfoList(CardMemberInfo cardMemberInfo)
    {
        return cardMemberInfoMapper.selectCardMemberInfoList(cardMemberInfo);
    }

    /**
     * 新增会员信息
     *
     * @param cardMemberInfo 会员信息
     * @return 结果
     */
    @Override
    public int insertCardMemberInfo(CardMemberInfo cardMemberInfo)
    {
        cardMemberInfo.setCreateTime(DateUtils.getNowDate());
        return cardMemberInfoMapper.insertCardMemberInfo(cardMemberInfo);
    }

    /**
     * 修改会员信息
     *
     * @param cardMemberInfo 会员信息
     * @return 结果
     */
    @Override
    public int updateCardMemberInfo(CardMemberInfo cardMemberInfo)
    {
        cardMemberInfo.setUpdateTime(DateUtils.getNowDate());
        return cardMemberInfoMapper.updateCardMemberInfo(cardMemberInfo);
    }

    /**
     * 批量删除会员信息
     *
     * @param cardIds 需要删除的会员信息主键
     * @return 结果
     */
    @Override
    public int deleteCardMemberInfoByCardIds(Long[] cardIds)
    {
        return cardMemberInfoMapper.deleteCardMemberInfoByCardIds(cardIds);
    }

    /**
     * 删除会员信息信息
     *
     * @param cardId 会员信息主键
     * @return 结果
     */
    @Override
    public int deleteCardMemberInfoByCardId(Long cardId)
    {
        return cardMemberInfoMapper.deleteCardMemberInfoByCardId(cardId);
    }

    @Override
    public int insertCardMemberInfoBatch(CardEmptycard cardEmptycard) {
        List<CardMemberInfo> infoList = new ArrayList<>();
        int end = Integer.parseInt(cardEmptycard.getCardnoEnd());
        int begin = Integer.parseInt(cardEmptycard.getCardnoBegin());
        CardScheme cardScheme = new CardScheme();
        cardScheme.setSchemePlan(cardEmptycard.getSchemePlan());
        for (int i = 0; i <= end - begin; i++) {
            CardMemberInfo cardMemberInfo = new CardMemberInfo();
            cardMemberInfo.setUserId(cardEmptycard.getUserId());
            cardMemberInfo.setDeptId(cardEmptycard.getDeptId());
            cardMemberInfo.setCreateBy(cardEmptycard.getCreateBy());
            cardMemberInfo.setSchemePlan(cardEmptycard.getSchemePlan());
            cardMemberInfo.setLsh(cardEmptycard.getLsh());
            cardMemberInfo.setCardType(String.valueOf(cardEmptycard.getCardType()));
            cardMemberInfo.setCardNo(String.valueOf(begin + i));
            cardMemberInfo.setCardState("0");
            cardMemberInfo.setCreateTime(DateUtils.getNowDate());
            cardMemberInfo.setIsbalance(cardEmptycard.getIsbalance());
            cardMemberInfo.setIsbalance(cardEmptycard.getIsscore());
            cardMemberInfo.setIsdiscount(cardEmptycard.getIsdiscount());
            cardMemberInfo.setIsequity(cardEmptycard.getIsequity());
            cardMemberInfo.setIstimes(cardEmptycard.getIstimes());
            infoList.add(cardMemberInfo);
        }
        return cardMemberInfoMapper.insertCardMemberInfoBatch(infoList);
    }

    @Override
    public int updateCardMemberInfoBatch(CardEmptycard cardEmptycard) {
        CardMemberInfo cardMemberInfo = new CardMemberInfo();
        cardMemberInfo.setLsh(cardEmptycard.getLsh());
        cardMemberInfo.setSchemePlan(cardEmptycard.getSchemePlan());
        cardMemberInfo.setCardType(String.valueOf(cardEmptycard.getCardType()));
        return cardMemberInfoMapper.updateCardMemberInfoBatch(cardMemberInfo);
    }

    @Override
    public int deleteCardMemberInfoBatch(List<CardEmptycard> cardEmptycards) {
        List<String> lshs = new ArrayList<>();
        for (CardEmptycard cardEmptycard : cardEmptycards) {
            lshs.add(cardEmptycard.getLsh());
        }
//        return cardMemberInfoMapper.deleteCardMemberInfoBatchByLshs(lshs);
        return cardMemberInfoMapper.updateCardMemberInfoBatchByLshs(lshs);
    }

    @Override
    public int updateCardMemberInfoByCardIds(Long[] cardIds, String cardState) {
        return cardMemberInfoMapper.updateCardMemberInfoByCardIds(cardIds, cardState);
    }
}
